%%% This m file is for transfering the data in continuous time into
%%% discrete years, which is good reference and used as the initial guesses
%%% in the discrete time model.


clear all
close all


load res_conti_full.mat

% # of the obervations in continuous time model
lengthf = length(tf);
lengthr = length(tr);


% Transfer into # of years in discrete time

Tbar_0 = round(Tbar);
T0_0 = round(T0);

yearsr0 = Tbar_0-T0_0;


% Values of s.v. in continuous time model
stater_conti = [kr,Hr];
statef_conti = [kf,Sf,Nf,g];

% Trasfer values into s.v. in discrete time

% Define the pickup rules of fossil regime
pickrule = linspace(1,lengthf,T0_0);
pickrule = repmat(round(pickrule),4,1);
pickmat = repmat([0;lengthf;2*lengthf;3*lengthf],1,T0_0);
pickrule = reshape(pickrule + pickmat,[],1);

% state variables k, S, and N in discrete time
statef = statef_conti(pickrule);
statef = reshape(statef, 4, T0_0)';

% Define the pickup rules of renew regime

pickrule = linspace(1,lengthr,yearsr0);
pickrule = repmat(round(pickrule),2,1);

pickrule(:,end) = pickrule(:,end)-1;
pickmat = repmat([0;lengthr],1,yearsr0);

pickrule = reshape(pickrule + pickmat,[],1);


% state variables k and H in discrete time.
stater = stater_conti(pickrule);
stater = reshape(stater, 2, yearsr0)';

% Speicify the variables

% Renewable regime
kr0 = stater(:,1);
Hr0 = stater(:,2);

kr0nxt = kr0(1:(length(kr0)-1));
ir0 = kr0nxt-(1-delta)*kr0(2:end);
k_Tbar1 = gamma*Kbar^(-1/gamma)*exp(-Abar*(Tbar+1)/gamma)/(beta*gamma-Abar*(gamma-1));
i_Tbar = k_Tbar1-(1-delta)*kr0(1);
ir0 = [i_Tbar;ir0];

Hr0nxt = Hr0(1:(length(Hr0)-1));
jr0 = ((Hr0nxt-Hr0(2:end))./(A.*kr0(2:end)).^psi).^(1/(1-psi));
jr0 = [0;jr0];

% Fossil fuel regime

kf0 = statef(:,1);
Sf0 = statef(:,2);
Nf0 = statef(:,3);
gf0 = statef(:,4);


kf0nxt = kf0(1:(length(kf0)-1)); 
if0 = kf0nxt-(1-delta)*kf0(2:end);
if0 = [ir0(end);if0];

Nf0nxt = Nf0(1:(length(Nf0)-1)); 
nf0 = Nf0nxt-Nf0(2:end);
nf0 = [0;nf0];

 save sv_discrete.mat T0_0 Tbar_0 kf0 Sf0 Nf0 if0 nf0 gf0 kr0 Hr0 ir0 jr0 

